<?php session_start(); ?>
<?php include_once('misc/class_config.php'); ?>
<?php
	include_once('access_control/page_access.php');
	declinePageIfNotLoggedIn();
	declinePageIfNotAdmin();
	
	$currentUser = new user();
	$currentUser->isLoggedIn();
	$currentUser->isTimedOut();
?>


<?php
	include_once('model/category.php');

	$newCategory = new category();
	$categoryMessage = '';
	
	$categoryNameErrorMessage = '';
	$categoryErrorMessage = '';
	$unitPriceErrorMessage = '';
	$descriptionErrorMessage = '';
	$quantityErrorMessage = '';
	
	if(isset($_POST['btnUpdateCategory']))
	{
		$categoryValidationError = new validation();

		$categoryNameErrorMessage = $categoryValidationError->isEmpty(trim($_POST['name']),'<label class="required">First name is required.</label>');
		$descriptionErrorMessage = $categoryValidationError->isEmpty(trim($_POST['description']),'<label class="required">Description is required.</label>');

		$newCategory->setCategory('',$_POST['name'],$_POST['description']);		
	}
?>

<?php
	if(isset($_POST['btnUpdateCategory']))
	{
		$formError = $categoryValidationError->status;
		
		if($formError==0)
		{
			try
			{
				if($newCategory->updateCategory())
				{
					$categoryMessage = '<span class="success_msg_span">Category udpated successfully.</span>';
				}
				else
				{
					$categoryMessage = '<span class="failed_msg_span">An error occured. Category was not added.</span>';
				}
			}
			catch(Exception $ex)
			{
				$categoryMessage = '<span class="failed_msg_span">'.$ex->getMessage().'</span>';
			}
		}
	}
?>

<?php $pageTitle = "Add Category | Everything"; ?>
<?php include_once('layout/page_start.php'); ?>
<?php include_once('layout/page_header.php'); ?>
<?php include_once('layout/menu_bar.php'); ?>

<div id="main_content_div">
<?php 
	if($productExist)
	{
		$categoryMessage = '<label>Product could not be found.</label>';
		echo '<div class="page_action_msg_div"><p>'.$editMessage.'</p></div>';
	}
	else
	{
		include_once('layout/editcategoryform.php'); 
	}
?>

</div>
<?php include('layout/page_footer.php'); ?>
<?php include('layout/page_end.php'); ?>